/**
 * @author Vannara
 */
package exist;

import java.io.IOException;
import java.util.ArrayList;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xmldb.api.base.Resource;
import org.xmldb.api.base.ResourceIterator;
import org.xmldb.api.base.ResourceSet;
import org.xmldb.api.base.XMLDBException;
import common.*;

public class QueryCountry{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static NodeList list;
	private static ConnectToeXist con;
	//Default Constructor
	public QueryCountry() throws ParserConfigurationException, SAXException, IOException{
		
	}
	
/**
 * Query all countries, put in List<Country> and return it
 * @return list of country
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws IOException
 * @throws ClassNotFoundException
 * @throws InstantiationException
 * @throws IllegalAccessException
 * @throws XMLDBException
 */
	public static ArrayList<Country> getAllCountries() throws ParserConfigurationException, SAXException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, XMLDBException{
		
		ArrayList<Country> countries = new ArrayList<Country>();
		//con=new ConnectToeXist();
		//con.connect();
		ConnectToeXist.connect();			  
	    ResourceSet result =con.getService().query("for $country in /mondial/country/name order by $country return $country/text()");
	    ResourceIterator i = result.getIterator();	       
	    while(i.hasMoreResources()) {	    	   
	        Resource r = i.nextResource();	  
	        Country country=new Country();
	        country.setName((String) r.getContent());
	        if(!countries.contains(country.getName())){
	        	countries.add(country);	
	        }	        
	    }			
		return countries;
	}
	
/**
 * return the information of country by a given name
 * return as html format
 * @param countryName
 * @return as HTML format
 * @throws ClassNotFoundException
 * @throws InstantiationException
 * @throws IllegalAccessException
 * @throws XMLDBException
 */
	public static String getCountryInfo(String countryName) throws ClassNotFoundException, InstantiationException, IllegalAccessException, XMLDBException{
		String html=null;
		String query=null;
		//con.connect();
		ConnectToeXist.connect();
		query="for $c in (//mondial/country) " +
			  "let $name :=$c/name " +
			  "let $pop :=$c/population " +
			  "let $pop_grow :=$c/population_growth " +
			  "let $gov :=$c/government " +
			  "let $gdp :=$c/gdp_total " +
			  "where $name='" + countryName + "'" +
			  "return <html><body><h3>{$name}</h3>" +
			  "<ul><li>population:{$pop}</li>" +
			  "<li>population_growth:{$pop_grow}</li> " +
			  "<li>government:{$gov}</li>" +
			  "<li>GDP:{$gdp}</li></ul> " +
			  "</body></html>";
	    ResourceSet result =con.getService().query(query);
	    ResourceIterator i = result.getIterator();	       
	    while(i.hasMoreResources()) {	    	   
	          Resource r = i.nextResource();
	          html =(String)r.getContent();	         
	       	}
	    return html.replaceAll("(\\r|\\n)", "");
	}
	
/**
 * Query all Cities that is in the county
 * @param countryName
 * @return list of city
 * @throws XMLDBException
 * @throws ClassNotFoundException
 * @throws InstantiationException
 * @throws IllegalAccessException
 */
	public static ArrayList<City> getAllCities(String countryName) throws XMLDBException, ClassNotFoundException, InstantiationException, IllegalAccessException{
		ArrayList<City> cities = new ArrayList<City>();
		
		//Query here		
		String query=null;		
		ConnectToeXist.connect();
		query="for $c in (//mondial/country) " +
			  "let $city := $c/city " +
			  "where $c/name='" +  countryName +  "' " +
			  "return $city";
	    ResourceSet result =con.getService().query(query);
	    ResourceIterator i = result.getIterator();	       
	    while(i.hasMoreResources()) {	
	    	  City city=new City();
	          Resource r = i.nextResource();	
	          city.setName((String) r.getContent());
	          cities.add(city);
	         // System.out.println(r.getContent());
	       	}
		
		return cities;
	}
	
	/**
	 * Get all city within a given country 
	 * @param countryName
	 * @return as XML format
	 * @throws XMLDBException
	 * @throws ClassNotFoundException
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 */
	public static String getAllCitiesAsXML(String countryName) throws XMLDBException, ClassNotFoundException, InstantiationException, IllegalAccessException{
		String xml=null;
		
		//Query here		
		String query=null;		
		ConnectToeXist.connect();
		query="for $c in (//mondial/country) " +
			  "let $city := $c/city " +
			  "where $c/name='" +  countryName +  "' " +
			  "return $city";
	    ResourceSet result =con.getService().query(query);
	    ResourceIterator i = result.getIterator();	       
	    while(i.hasMoreResources()) {	
	    	  City city=new City();
	          Resource r = i.nextResource();	
	          xml=(String) r.getContent();
	       	}
		
		return xml;
	}
	
/**
 * Query all cities name within a given country name
 * @param countryName
 * @return list of city
 * @throws XMLDBException
 * @throws ClassNotFoundException
 * @throws InstantiationException
 * @throws IllegalAccessException
 */
	public static ArrayList<City> getAllCityNameByCountryName(String countryName) throws XMLDBException, ClassNotFoundException, InstantiationException, IllegalAccessException{
		ArrayList<City> cities =new ArrayList<City>();
		String query=null;		
		ConnectToeXist.connect();
		query="for $c in (//mondial/country) " +
			  "let $city := $c/city " +
			  "where $c/name='" + countryName +  "' " +
			  "return $city/name/text() ";
	    ResourceSet result =con.getService().query(query);
	    ResourceIterator i = result.getIterator();	       
	    while(i.hasMoreResources()) {	
	    	  City city=new City();
	          Resource r = i.nextResource();	          
	          if(!cities.contains(r.getContent())){
	        	  city.setName((String) r.getContent());
	        	  cities.add(city);	        	  	  
	          }	         
	       	}
		return cities;
	}
	
/**
 * Query all provinces name within a given country name
 * @param countryName
 * @return list of province
 * @throws XMLDBException
 * @throws ClassNotFoundException
 * @throws InstantiationException
 * @throws IllegalAccessException
 */
	public static ArrayList<Province> getAllProvinceNameByCountryName(String countryName) throws XMLDBException, ClassNotFoundException, InstantiationException, IllegalAccessException{
		ArrayList<Province> provinces = new ArrayList<Province>();
		
		//Query here		
		String query=null;		
		ConnectToeXist.connect();
		query="for $c in (//mondial/country) " +
			  "let $pro := $c/province " +
			  "where $c/name='" +  countryName +  "' " +
			  "return $pro/name/text()";
	    ResourceSet result =con.getService().query(query);
	    ResourceIterator i = result.getIterator();	       
	    while(i.hasMoreResources()) {	
	    	  Province province=new Province();
	          Resource r = i.nextResource();	
	          province.setName((String) r.getContent());
	          provinces.add(province);		         
	       	}
		
		return provinces;
	}	
	
/**
 * Query all cities name within a given province name
 * @param provinceName
 * @return List of city
 * @throws XMLDBException
 * @throws ClassNotFoundException
 * @throws InstantiationException
 * @throws IllegalAccessException
 */
	public static ArrayList<City> getAllCityNameByProvinceName(String provinceName) throws XMLDBException, ClassNotFoundException, InstantiationException, IllegalAccessException{
		ArrayList<City> cities = new ArrayList<City>();
		
		//Query here		
		String query=null;		
		ConnectToeXist.connect();
		query= " for $province in (//mondial/country/province) " +
				"let $city :=$province/city " + 
				"where $province/name='" + provinceName + " ' " +
				"return $city/name/text()" ;
	    ResourceSet result =con.getService().query(query);
	    ResourceIterator i = result.getIterator();	       
	    while(i.hasMoreResources()) {	
	    	  City city=new City();
	          Resource r = i.nextResource();
	          if(!cities.contains(r.getContent())){
	        	  city.setName((String) r.getContent());
		          cities.add(city);  
	          }	          		         
	    }		
		return cities;
	}	
	
/**
 * Query province info (area, population)
 * @param provinceName
 * @return HTML format
 * @throws ClassNotFoundException
 * @throws InstantiationException
 * @throws IllegalAccessException
 * @throws XMLDBException
 */
	public static String getProvinceInfoAsHTML(String provinceName) throws ClassNotFoundException, InstantiationException, IllegalAccessException, XMLDBException{
		ConnectToeXist.connect();
		String html=null;
		String query=	"for $province in (//mondial/country/province) " +
						"let $name :=$province/name " +
						"let $pop :=$province/population " + 
						"let $area :=$province/area " +
						"where $name='" + provinceName + "' " +
						"return <html><body><h3>{$name}</h3> " +
						"<ul><li>population:{$pop}</li> " +
						"<li>area:{$area}</li></ul> " +
						"</body></html>";
		 ResourceSet result =con.getService().query(query);
		 ResourceIterator i = result.getIterator();	       
	     while(i.hasMoreResources()) {	    	   
	          Resource r = i.nextResource();
	          html =(String)r.getContent();	         
	       	}
		 return html.replaceAll("(\\r|\\n)", "");
	}		
	
	/***
	 * Get capital city of a given country name
	 * @param countryName
	 * @return name of capital city
	 * @throws ClassNotFoundException
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws XMLDBException
	 */
	public static String getCapitalCityofCountry(String countryName) throws ClassNotFoundException, InstantiationException, IllegalAccessException, XMLDBException{
		String capital=null;
		ConnectToeXist.connect();
		String query=   "for $c in (//mondial/country) " +
						"let $l := $c//city[fn:matches(@is_country_cap,'yes')] " +
						"where $c/name='" + countryName + "'" +
						"order by $c" +
						"return $l/name/text()";
		 ResourceSet result =con.getService().query(query);
		 ResourceIterator i = result.getIterator();	       
	     while(i.hasMoreResources()) {	    	   
	          Resource r = i.nextResource();
	          capital =(String)r.getContent();	         
	       	}
	     return capital;
	}
	
	/***
	 * get city information within a given province name
	 * @param provinceName
	 * @return as HTML format
	 * @throws ClassNotFoundException
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws XMLDBException
	 */
	public static String getCityInfoInProvinceAsHTML(String cityName) throws ClassNotFoundException, InstantiationException, IllegalAccessException, XMLDBException{
		ConnectToeXist.connect();
		String html=null;
		String query=	"for $province in (//mondial/country/province) " +
						"let $name :=$province/name " +
						"let $pop :=$province/population " + 
						"let $area :=$province/area " +
						"where $name='" + cityName + "' " +
						"return <html><body><h3>{$name}</h3> " +
						"<ul><li>population:{$pop}</li> " +
						"<li>area:{$area}</li></ul> " +
						"</body></html>";
		 ResourceSet result =con.getService().query(query);
		 ResourceIterator i = result.getIterator();	       
	     while(i.hasMoreResources()) {	    	   
	          Resource r = i.nextResource();
	          html =(String)r.getContent();	         
	       	}
		 return html.replaceAll("(\\r|\\n)", "");
	}
	
	/***
	 * get city information within a given province name
	 * @param provinceName
	 * @return as HTML format
	 * @throws ClassNotFoundException
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws XMLDBException
	 */
	public static String getCityInfoInCountryAsHTML(String cityName) throws ClassNotFoundException, InstantiationException, IllegalAccessException, XMLDBException{
		ConnectToeXist.connect();
		String html=null;
		String query=	"for $c in (//mondial/country/city) " +
						"let $name :=$c/name " +
						"let $pop :=$c/population " +
						"where $name='"+ cityName + "' " +
						"return <html><body><h3>{$name/text()}</h3> "+ 
						"<ul><li>population:{$pop/text()}</li></ul></body></html>"; 
		 ResourceSet result =con.getService().query(query);
		 ResourceIterator i = result.getIterator();	       
	     while(i.hasMoreResources()) {	    	   
	          Resource r = i.nextResource();
	          html =(String)r.getContent();	         
	       	}
		 return html.replaceAll("(\\r|\\n)", "");
	}
	
	/***
	 * 
	 * @param args
	 * @throws ClassNotFoundException
	 * @throws XMLDBException
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws ParserConfigurationException
	 * @throws SAXException
	 * @throws IOException
	 */
	public static void main (String []args) throws ClassNotFoundException, XMLDBException, InstantiationException, IllegalAccessException, ParserConfigurationException, SAXException, IOException
	{
		ArrayList<City> cs=new ArrayList<City>();
		cs=getAllCities("Albania");
		for(City c:cs){
			System.out.println(c.getName());
		}
	}
	
}
